//////////////////////////////////////////////////////////////////////////

#ifndef _GRAPHICSIMPL_H_
#include "graphicsimpl.h"
#endif

//////////////////////////////////////////////////////////////////////////

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

//////////////////////////////////////////////////////////////////////////


GraphicsImpl::GraphicsImpl()
{
    // Init SDL
    SDL_Init( SDL_INIT_EVERYTHING );

    const int width = 640;
    const int height = 480;
    const int bitsPerPixel = 32;

    //Set up the screen
    m_Screen = SDL_SetVideoMode( width, height, bitsPerPixel, SDL_HWSURFACE | SDL_DOUBLEBUF);
}


//////////////////////////////////////////////////////////////////////////


GraphicsImpl::~GraphicsImpl()
{
    SDL_Quit();
}


//////////////////////////////////////////////////////////////////////////


Graphics::Object GraphicsImpl::LoadObject( const std::string& _fileName )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( _fileName.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old surface
        SDL_FreeSurface( loadedImage );

        //If the surface was optimized
        if( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0xFF, 0xFF, 0xFF ) );
        }
    }

    //Return the optimized surface
    return optimizedImage;
}


//////////////////////////////////////////////////////////////////////////


void GraphicsImpl::DrawObject( Graphics::Object _object, size_t _x, size_t _y )
{
    //Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = _x;
    offset.y = _y;

    //Blit
    SDL_BlitSurface( static_cast<SDL_Surface*>(_object),
                     NULL,
                     static_cast<SDL_Surface*>(m_Screen),
                     &offset );
}


//////////////////////////////////////////////////////////////////////////


void GraphicsImpl::Update()
{
    // Update the screen
    SDL_Flip( static_cast<SDL_Surface*>(m_Screen) );
}


//////////////////////////////////////////////////////////////////////////
